package wsclients;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory;
import org.jdom.Element;
import org.jdom.Namespace;

import com.avetti.commerce.domainmodel.OrderItem;
import com.avetti.commerce.webservices.OrderService;

public class OrderServiceTest {

    static {
        ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
        Protocol protocol = new Protocol("https", easy, 443);
        Protocol.registerProtocol("https", protocol);
    }
    
    public final static int ACTION_CANCELLED = 1;
	public final static int ACTION_EXCHANGED = 2;
	public final static int ACTION_RETURNED = 3;
	public final static int ACTION_TO_BE_SHIPPED = 4;
	public final static int ACTION_SHIPPED = 5;
	public final static int ACTION_PROCESSED = 6;
	public final static int ACTION_ON_HOLD = 7;
	public final static int ACTION_APPROVE = 8;
	public final static int ACTION_REJECT  = 9;
    
    public void out(Object obj){
    	System.out.println(obj);
    }
    public void testlocal(String[] args) throws Exception {
        
        
        String vid="20070723002";
        String admin="weisun_ws";
        String pwd="1234";
        
        Long orderdataid=760L;
        long orderitemid1=1102;
        //long orderitemid2=1042;
        long qty=1;

        String url = "http://192.168.1.100/t80devnew/services/orderservice";

        Service serviceModel = new ObjectServiceFactory().create(OrderService.class);
        OrderService service = (OrderService) new XFireProxyFactory().create(
                serviceModel, url); 

        Client client = Client.getInstance(service);
        client.addOutHandler(new VendorIdHandler(vid));
        client.addOutHandler(new UserPassHandler(admin, pwd));
        
        Long orderDataId=1779L;
        List<OrderItem> re=service.findOrderItems(orderDataId);
        out("Size:"+re.size());
        for(OrderItem i : re){
        	out(i.getOrderitemid()+" "+i.getVid());
        }
    }

    private class VendorIdHandler extends AbstractHandler {
        private String vendorId;

        VendorIdHandler(String vendorId) {
            super();
            this.vendorId = vendorId;
        }

        public String getVendorId() {
            return vendorId;
        }

        public void setVendorId(String vendorId) {
            this.vendorId = vendorId;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:vendor");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element vendor = new Element("VendorToken", ns);
            Element vendor_el = new Element("VendorId", ns);
            vendor_el.addContent(vendorId);
            vendor.addContent(vendor_el);
            header.addContent(vendor);
        }
    }

    private class UserPassHandler extends AbstractHandler {
        private String user;
        private String pass;

        UserPassHandler(String user, String pass) {
            super();
            this.user = user;
            this.pass = pass;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:authentication");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element root = new Element("AuthenticationToken", ns);
            Element username_el = new Element("Username", ns);
            Element password_el = new Element("Password", ns);

            username_el.addContent(user);
            password_el.addContent(pass);
            root.addContent(username_el);
            root.addContent(password_el);

            header.addContent(root);
        }
    }

    public static void main(String[] args) throws Exception {
        OrderServiceTest cst=new OrderServiceTest();
        //cst.test(args);
        cst.testlocal(args);
    }
}
